//
//  AlertHistoryView.swift
//  HisPaste
//
//  Created by 烟雀 on 2024/2/16.
//

import Foundation
import SwiftUI


struct AlertHistoryView: View{
    var body: some View{
        ZStack{
            HistoryView()
                .background(RoundedRectangle(cornerRadius: 15,
                                             style: .continuous)
                    .foregroundColor(Color(hex: 0x252627))
                )
        }
        .font(Font.system(size: 20.0))
//        .background(Color.brown.opacity(0.3).blur(radius: 10))
        .frame(width: 400, height: 400)
        .padding(.top, 40.0)
//        .edgesIgnoringSafeArea(.all)
        .background(
                        RoundedRectangle(cornerRadius: 15, style: .continuous)
                            .frame(width: 400, height: 400)
                            .foregroundColor(Color.black) // 设置背景颜色
                            .padding(.top, 40)
                    )
        .onReceive(NotificationCenter.default.publisher(for: NSWindow.didResignMainNotification)) { _ in
            AlertHistoryView.tryCloseThis()
        }
    
    }
    
    static var alertWindow: NSWindow?
    static func tryCloseThis(){
        alertWindow?.close()
    }
}

extension AlertHistoryView{
    // MARK: - Interface
    
    func showViewOnNewWindowInSpecificTime(during timer: CGFloat) -> NSWindow {
        let alertWindow = self.setWindow()
        displayAsAlert(win: alertWindow, Timer: timer)
        return alertWindow
    }
    
    func showViewOnNewWindow() -> NSWindow {
        let alertWindow = self.setWindow()
        AlertHistoryView.alertWindow = alertWindow
        displayAsAlert(win: alertWindow, Timer: nil)
        return alertWindow
    }
    
    
    // MARK: - Attribute
    
    private func displayAsAlert(win:NSWindow, Timer:Double?) {
        
        // 在当前窗口上显示
        // win.level = .floating
    
        
        // 设置透明
        // win.isOpaque = false
        // win.backgroundColor = NSColor.clear
        // win.hasShadow = false  // 可选：如果不想要窗口阴影
        // win.alphaValue = 0.1
        
        win.isMovableByWindowBackground = true
        win.titleVisibility = .hidden
        win.titlebarAppearsTransparent = true
        win.isOpaque = false
        win.styleMask.remove(.closable)
        win.styleMask.remove(.fullScreen)
        win.styleMask.remove(.miniaturizable)
        win.styleMask.remove(.fullSizeContentView)
        win.styleMask.remove(.resizable)
        win.backgroundColor = NSColor.clear
        win.orderFrontRegardless()
        
        // win.makeKeyAndOrderFront(nil)
        // 设置焦点到最新打开的窗口, fix全屏时候焦点不会自动切
        NSApplication.shared.activate(ignoringOtherApps: true)
        
//         DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
//             win.close()
//         }
    }
    
    private func setWindow() -> NSWindow {
        NSWindow(contentViewController: NSHostingController(rootView: self))
    }
    
}


// 用不了 windowDidResignMain, 貌似底层转换存在问题

//@objcMembers
//class HostingControllerDelegate: NSObject, NSWindowDelegate {
//    var alertWin: NSWindow?
//    
//    func windowDidResignMain(_ notification: Notification) {
//        print("Window resigned key (clicked outside the window)!")
//        alertWin?.close()
//    }
//    
//    init(window: NSWindow) {
//        super.init()
//        self.alertWin = window
//    }
//    
//}




